package com.jdd.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdd.common.exception.JddBootException;
import com.jdd.common.system.api.ISysBaseAPI;
import com.jdd.common.system.vo.DictModel;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.IPUtils;
import com.jdd.common.util.SpringContextUtils;
import com.jdd.common.util.oConvertUtils;
import com.jdd.modules.system.entity.SysDict;
import com.jdd.modules.system.entity.SysLog;
import com.jdd.modules.system.entity.SysUser;
import com.jdd.modules.system.mapper.SysLogMapper;
import com.jdd.modules.system.mapper.SysUserMapper;
import com.jdd.modules.system.mapper.SysUserRoleMapper;
import com.jdd.modules.system.service.ISysDictService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 底层共通业务API，提供其他独立模块调用
 * @Author: scott
 * @Date:2019-4-20
 * @Version:V1.0
 */
@Slf4j
@Service
public class SysBaseApiImpl implements ISysBaseAPI {
	public static final String DB_TYPE_MYSQL="MYSQL";
	public static final String DB_TYPE_ORACLE="ORACLE";
	public static final String DB_TYPE_POSTGRESQL="POSTGRESQL";
	public static final String DB_TYPE_SQLSERVER="SQLSERVER";
	public static String DB_TYPE = "";

	@Resource
	private SysLogMapper sysLogMapper;
	@Resource
	private SysUserMapper userMapper;
	@Resource
	private SysUserRoleMapper sysUserRoleMapper;
	@Resource
	private ISysDictService sysDictService;
//	@Resource
//	private SysAnnouncementMapper sysAnnouncementMapper;
//	@Resource
//	private SysAnnouncementSendMapper sysAnnouncementSendMapper;

	@Override
	public void addLog(String LogContent, Integer logType, Integer operatetype) {
		SysLog sysLog = new SysLog();
		//注解上的描述,操作日志内容
		sysLog.setLogContent(LogContent);
		sysLog.setLogType(logType);
		sysLog.setOperateType(operatetype);

		//请求的方法名
		//请求的参数

		try {
			//获取request
			HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
			//设置IP地址
			sysLog.setIp(IPUtils.getIpAddr(request));
		} catch (Exception e) {
			sysLog.setIp("127.0.0.1");
		}

		//获取登录用户信息
		LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
		if(sysUser!=null){
			sysLog.setUserid(sysUser.getUsername());
			sysLog.setUsername(sysUser.getRealname());

		}
		sysLog.setCreateTime(new Date());
		//保存系统日志
		sysLogMapper.insert(sysLog);
	}

	@Override
	public LoginUser getUserByName(String username) {
		if(oConvertUtils.isEmpty(username)) {
			return null;
		}
		LoginUser loginUser = new LoginUser();
		SysUser sysUser = userMapper.getUserByName(username);
		if(sysUser==null) {
			return null;
		}
		BeanUtils.copyProperties(sysUser, loginUser);
		return loginUser;
	}

	@Override
	public List<String> getRolesByUsername(String username) {
		return sysUserRoleMapper.getRoleByUserName(username);
	}

	@Override
	public String getDatabaseType() throws SQLException {
		DataSource dataSource = SpringContextUtils.getApplicationContext().getBean(DataSource.class);
		return getDatabaseTypeByDataSource(dataSource);
	}

	@Override
	public List<DictModel> queryDictItemsByCode(String code) {
		return sysDictService.queryDictItemsByCode(code);
	}

	@Override
	public List<DictModel> queryTableDictItemsByCode(String table, String text, String code) {
		return sysDictService.queryTableDictItemsByCode(table, text, code);
	}

	@Override
	public List<DictModel> queryAllDepartBackDictModel() {
		return sysDictService.queryAllDepartBackDictModel();
	}

	@Override
	public void sendSysAnnouncement(String fromUser, String toUser, String title, String msgContent) {
//		SysAnnouncement announcement = new SysAnnouncement();
//		announcement.setTitile(title);
//		announcement.setMsgContent(msgContent);
//		announcement.setSender(fromUser);
//		announcement.setPriority(CommonConstant.PRIORITY_M);
//		announcement.setMsgType(CommonConstant.MSG_TYPE_UESR);
//		announcement.setSendStatus(CommonConstant.HAS_SEND);
//		announcement.setSendTime(new Date());
//		announcement.setMsgCategory(CommonConstant.MSG_CATEGORY_2);
//		announcement.setDelFlag(String.valueOf(CommonConstant.DEL_FLAG_0));
//		sysAnnouncementMapper.insert(announcement);
//		// 2.插入用户通告阅读标记表记录
//		String userId = toUser;
//		String[] userIds = userId.split(",");
//		String anntId = announcement.getId();
//		for(int i=0;i<userIds.length;i++) {
//			if(oConvertUtils.isNotEmpty(userIds[i])) {
//				SysUser sysUser = userMapper.getUserByName(userIds[i]);
//				if(sysUser==null) {
//					continue;
//				}
//				SysAnnouncementSend announcementSend = new SysAnnouncementSend();
//				announcementSend.setAnntId(anntId);
//				announcementSend.setUserId(sysUser.getId());
//				announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
//				sysAnnouncementSendMapper.insert(announcementSend);
//			}
//		}
	}
	/**
	 * 获取数据库类型
	 * @param dataSource
	 * @return
	 * @throws SQLException
	 */
	private String getDatabaseTypeByDataSource(DataSource dataSource) throws SQLException{
		if("".equals(DB_TYPE)) {
			Connection connection = dataSource.getConnection();
			try {
				DatabaseMetaData md = connection.getMetaData();
				String dbType = md.getDatabaseProductName().toLowerCase();
				if (dbType.indexOf("mysql") >= 0) {
					DB_TYPE = DB_TYPE_MYSQL;
				} else if (dbType.indexOf("oracle") >= 0) {
					DB_TYPE = DB_TYPE_ORACLE;
				} else if (dbType.indexOf("sqlserver") >= 0 || dbType.indexOf("sql server") >= 0) {
					DB_TYPE = DB_TYPE_SQLSERVER;
				} else if (dbType.indexOf("postgresql") >= 0) {
					DB_TYPE = DB_TYPE_POSTGRESQL;
				} else {
					throw new JddBootException("数据库类型:[" + dbType + "]不识别!");
				}
			} catch (Exception e) {
				log.error(e.getMessage(), e);
			} finally {
				connection.close();
			}
		}
		return DB_TYPE;

	}

	@Override
	public List<DictModel> queryAllDict() {
		// 查询并排序
		QueryWrapper<SysDict> queryWrapper = new QueryWrapper<SysDict>();
		queryWrapper.orderByAsc("create_time");
		List<SysDict> dicts = sysDictService.list(queryWrapper);
		// 封装成 model
		List<DictModel> list = new ArrayList<DictModel>();
		for (SysDict dict : dicts) {
			list.add(new DictModel(dict.getDictCode(), dict.getDictName()));
		}

		return list;
	}
}
